package top.infopub.mgr.cap.web;

import com.alibaba.fastjson.JSON;
import org.apache.commons.lang3.StringEscapeUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import top.infopub.dto.mkt.DetailTrackDto;
import top.infopub.enums.cap.CapProcessDetailTypeEnum;
import top.infopub.enums.mkt.ProProcesssStatusEnum;
import top.infopub.mgr.cap.service.CapProcessManageService;
import top.infopub.mgr.cap.service.FundingApprovalService;
import top.infopub.mgr.common.vo.RspVo;
import top.infopub.mgr.flow.service.OAFlowService;
import top.infopub.mgr.mkt.service.ProProcessManageService;
import top.infopub.model.cap.FinancingTracking;
import top.infopub.security.util.SessionUserUtils;
import top.infopub.security.vo.AuthorizationVO;

import javax.servlet.http.HttpServletRequest;

/**
 * @Author Warking
 * @Date 2021/6/6 21:15
 * @Version 1.0  融资跟踪  fundingApprovalTrace
 */
@Controller
@RequestMapping("/financingTracking")
public class FundingApprovalTraceController {

    @Autowired
    private FundingApprovalService fundingApprovalService;
    @Autowired
    private OAFlowService oaFlowService;
    @Autowired
    private CapProcessManageService capProcessManageService;

    /**
     * 融资跟踪详情跳转
     *
     * @return
     */
    @RequestMapping("/toDealOrDetail")
    public String toDealOrDetailTrace(HttpServletRequest request, String id, String setupFinancialId, String proCode, String proName) {
        request.setAttribute("proCode", proCode);
        request.setAttribute("proName", proName);
        request.setAttribute("id", id);
        request.setAttribute("setupFinancialId", setupFinancialId);
        request.setAttribute("isAuditFlag", "1");
        request.setAttribute("auditTabFlag", "0");
        AuthorizationVO vo = SessionUserUtils.getSessionAttributeForUserDtl();
        /*查询融资跟踪数据*/
        FinancingTracking financingTracking  = fundingApprovalService.selectById(id);

        if (financingTracking != null) {
            if (StringUtils.equals(ProProcesssStatusEnum.PRO_STATUS1.getCode(), financingTracking.getProcessStatus())
                    || StringUtils.equals(ProProcesssStatusEnum.PRO_STATUS9.getCode(), financingTracking.getProcessStatus())) {
                request.setAttribute("isAuditFlag", "0");
            }
            if (StringUtils.isNotBlank(financingTracking.getProcessId()) && financingTracking.getProcessId().length() > 10) {
                request.setAttribute("processId", financingTracking.getProcessId());
                String auditTabFlag = capProcessManageService.checkShowAuditTab(financingTracking.getProcessId(), vo);
                if (StringUtils.equals(ProProcesssStatusEnum.PRO_STATUS1.getCode(), financingTracking.getProcessStatus())
                        && StringUtils.equals("0", auditTabFlag)) {
                    request.setAttribute("isAuditFlag", "0");
                }
                request.setAttribute("auditTabFlag", auditTabFlag);
            }
        }

        return "cap/lay-FinancingTracking-detail";
    }

    @RequestMapping("/auditProcess")
    @ResponseBody
    public RspVo auditProcess(String id, String processId, String status, String auditMsg) {
        //立项审批
        capProcessManageService.auditProject(id, processId, status, auditMsg);
        return RspVo.getSuccessResponse();
    }

    //融资跟踪的详情
    @RequestMapping("/queryDetail")
    @ResponseBody
    public DetailTrackDto queryDetailTrack(String id, String setupFinancialId) {
        DetailTrackDto detailTrackDto = new DetailTrackDto();
        if (setupFinancialId != null && setupFinancialId != "") {

            detailTrackDto = fundingApprovalService.queryDetailTrack(setupFinancialId);
            return detailTrackDto;
        } else {
            //审批会传id
            FinancingTracking tracking = fundingApprovalService.findById(id);
            if (tracking !=null)
            {
                detailTrackDto = fundingApprovalService.queryDetailTrack(tracking.getSetupFinancialId());
                detailTrackDto.setFinancingTrackingApproval(tracking);
            }
            return detailTrackDto;
        }
    }


    //融资跟踪 保存提交 前端传一个融资跟踪的数据过来  proCode放到financingTracking 还有金融的SGUID
    @RequestMapping("/save")
    @ResponseBody
    public RspVo save(String financingTracking, String identifying, String proCode) {
        if (StringUtils.isEmpty(financingTracking)) {
            return RspVo.getFailureResponse(0, "数据为空");
        }

        String object = StringEscapeUtils.unescapeJava(financingTracking);
        FinancingTracking tracking = JSON.parseObject(object, FinancingTracking.class);
        //查询当前的跟踪数据的审批状态
        FinancingTracking flagFinancingTracking = fundingApprovalService.findById(tracking.getId());
        if (flagFinancingTracking != null) {
            if (flagFinancingTracking.getProcessStatus() == "1" || flagFinancingTracking.getProcessStatus() == "9") {
                return RspVo.getFailureResponse(0, "审批中，保存失败！");
            }
        }
        if ("0".equals(identifying)) {//0是保存
            String trackingId = fundingApprovalService.save(tracking);
            if (!StringUtils.isEmpty(trackingId)) {
                return RspVo.getSuccessResponse(0, "保存成功");
            } else {
                return RspVo.getFailureResponse(0, "保存失败");
            }
        } else {
            if (flagFinancingTracking != null) {
                if (StringUtils.isNotBlank(flagFinancingTracking.getOaId())) {
                    oaFlowService.finishOA(flagFinancingTracking.getOaId());
                }
            }
            //提交流程  先保存
            String trackingId = fundingApprovalService.save(tracking);
            if (!StringUtils.isEmpty(trackingId)) {//保存成的情况 走流程
                //登录人员信息
                AuthorizationVO vo = SessionUserUtils.getSessionAttributeForUserDtl();
                //发起流程
                String process = capProcessManageService.sendProcess(CapProcessDetailTypeEnum.PRO_NODE_TYPE6, object, trackingId, proCode, vo);
                if (!StringUtils.isEmpty(process)) {
                    return RspVo.getSuccessResponse(0, "提交成功");
                }
                return RspVo.getFailureResponse(0, "提交失败");
            } else { //提交-保存失败
                return RspVo.getFailureResponse(0, "提交失败");
            }

        }
    }

}
